<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\checkRequest;
use App\Http\Controllers\Controller;
use Hash;
use DB;
use Gregwar\Captcha\CaptchaBuilder;
use App\area;

class loginController extends Controller
{
    public function viewlogin()
    {

        $builder = new CaptchaBuilder;
        $res = $builder->build($width = 100, $height = 50);
        //将验证码session中
        session(['Captcha' => $builder->getPhrase()]);
        return view('login.sign_in',['Captcha' => $res]);
    } 
                    
    public function dologin(checkRequest $request)
    {

        if (session('Captcha') === $request -> Captcha) {

            $phone = $request -> phone;
            $password = $request -> password;

            $res = DB::table('tests') -> where('phone',$phone)-> first();

            if(empty($res)) {
                return back()-> with('info','密码输入不正确');
            }

            if(Hash::check($password,$res -> password)) {

                session(['id' => $res -> id]);
                return redirect('/list') -> with('info','登录成功');
            }

        } else {

            return back();
        }
    }

    //显示登录
    public function register()
    {
      return view('login.sign_up');
    } 

    //执行登录
    public function doRegister(checkRequest $request)
    {

        $nickname = $request -> nickname;
        $phone = $request -> phone;
        $password = $request -> password;

        $passwd = Hash::make($password);

        $res = DB::table('tests') -> insert([

                'nickname' => $nickname,
                'phone' => $phone,
                'password' => $passwd 
            ]);

        if($res) {

            return redirect('/login') -> with('info','注册成功');
        } else {

            return back() -> with('info','注册失败');
        }
    }

    public function viewlist()
    {   
        //获取数据
       $areas = area::get();

     $data = $this -> findSubTree($areas);
        return view('login.list',['data' => $data]);
    }

    public function loginout(Request $request)
    {

        $request->session()->flush();
        return redirect('/login');
    }

    public function doAdd(){

        $data = area::get();
        $data = $this -> tree($data);
        return view('login.area',['data' => $data]);
    }

    public function tree($cates,$id=0,$lev=0)
     {
       static $sub = [];
       foreach($cates as $v){
         if($v -> pid == $id){ 
           
           $v -> lev = $lev;
           $sub[] = $v;
           $this -> tree($cates,$v -> id,$lev+1);
         }            
       }
       return $sub;
     }

    public function findSubTree($cates,$id=0,$lev=1)
    {

          $subtree = [];

          foreach ($cates as $v) {

            if($v -> pid == $id){

                $v -> lev = $lev;
                $subtree[] = $v;
                $subtree = array_merge($subtree,$this -> findSubTree($cates,$v->id,$lev+1));
            }
          }

          return $subtree;
  Route::get('/list','loginController@viewlist');
          
    }
}
